//@charset "utf-8";
//问句数据
define(function(require, exports, module) {
	var Backbone = require('backbone');
	var _ = require('_');

	var clientJump = require('../lib/clientjump');

	var querySearchModel = require('../models/querysearch');
	var historyQueryModel = require('../models/historyquery');
	var codelistModel = require('../models/codelist');

	var secondSearchView = require('./secondsearch');
	var BaseView = require('./base');
	var baseView = new BaseView();

	var QuerySearchView = Backbone.View.extend({
		el: '#view_result_condition',
		query: '',
		pageSize: 10,
		initialize: function() {
			secondSearchView.querySearchView = this;
		},
		events: {
			'dblclick [view_dbl_query_search]': 'jumpClientMLine',
			'click .view_td_sort': 'stockSort'
		},
		request: function(query) {
			$('#noResultView').hide();
			$('#view_result_condition').hide();

			var self = this;

			self.query = query;
			if (!baseView.getQuery('plateid')) {
				codelistModel.clearCodeList();
			}
			query = secondSearchView.getSelectedOptions(query);
			querySearchModel.getQueryResult(query, function() {
				$('#view_searchinput').val(query);
				self.render();
				//COCO新增
				//判断有板块id时隐藏条件
				if (baseView.getQuery('plateid')) {
					$('#view_result_condition_create').hide();
					$('#view_select_choose').hide();
					$('#view_select_option_container').hide();
				}
			});
		},
		render: function() {
			var self = this;
			if (self.renderEmpty()) {
				return;
			}

			$('#view_result_condition_second').hide();

			historyQueryModel.saveHistoryData(self.query);



			var result = querySearchModel.get('result');
			var qid = result.qid;
			var token = result.token;
			var total = result.total;
			var page = 1;
			var conditionNumStr = '选出结果:<span class="cbla">' + total + '</span>个';
			self.pageSize = 10;

			$('#view_result_condition_create').show();
			$("#view_select_choose").hide();
			if (qid !== '' && token !== '') {
				$('#view_result_condition_second').show();
				self.pageSize = 6;
				conditionNumStr += ',超出300个限定,继续优化 ↓';
				secondSearchView.request(qid, token, self.query);
				$(".result-table").height('317px');
				$("#div_headfixed").addClass("table-headfixed-second");
			} else {
				$(".result-table").height('355px');
				$("#div_headfixed").removeClass("table-headfixed-second");
			}
			if ($('.view_second_option.selected').length >= 1) {
				$('#view_result_condition_second').show();
				self.pageSize = 6;
				$(".result-table").height('317px');
				$("#div_headfixed").addClass("table-headfixed-second");
			}

			$('#view_result_condition_num').html(conditionNumStr);
			self.renderData(page);
			$('#operate').show();
			self.$el.show();
			// $('.result-table').scrollTop(0);
			baseView.renderScrollTable();
		},
		renderData: function(page) {
			var self = this;
			var result = querySearchModel.get('result');
			var num = (page - 1) * self.pageSize + 1;
			var selectedCodeList = codelistModel.getCodeList();
			
			var html = [];
			for (var i in result.data) {
				var isSelected = false;
				if (1 === selectedCodeList[result.data[i]['code']]) {
					isSelected = true;
				}
				var zdfStr = '';
				if (result.data[i]['zhangdiefu'] === '--') {
					zdfStr = '<td class="green">--</td>';
				} else {
					zdfStr = '<td class="' + (result.data[i]['zhangdiefu'] > 0 ? 'red' : 'green') + '">' + result.data[i]['zhangdiefu'] + '%</td>';
				}

				var newprice = result.data[i]['newprice'];
				var row = [
					'<tr view_click_stat="cop_581208fa_505" class="data-stock ' + (isSelected ? 'data-stock-select' : '') + (num % 2 == 0 ? ' bggray' : '') + ' ' + '" ',
					'data-index="' + num + '" data-stock="' + result.data[i]['code'] + '" ',
					'view_dbl_query_search="' + result.data[i]['code'] + '">',
					'<td style=" text-align: right;"><input type="checkbox" class="selectAll selectRows" ' + (isSelected ? 'checked' : '') + ' value_code="' + result.data[i]['code'] + '"></td>',
					'<td>' + num + '</td>',
					'<td>' + result.data[i]['code'] + '</td>',
					'<td>' + result.data[i]['zqmc'] + '</td>',
					zdfStr,
					'<td class="' + (result.data[i]['zhangdiefu'] > 0 ? 'red' : 'green') + '">' + newprice.toFixed(2) + '</td>',
					'</tr>'
				];
				num++;

				html.push(row.join(''));
			}
			$('#view_result_condition_table_tbody').html(html.join(''));

		},
		renderEmpty: function() {
			var errorCode = querySearchModel.get('errorcode');
			var result = querySearchModel.get('result');
			// alert(JSON.stringify(result));
			var isSecond = false;

			if ($('.view_second_option.selected').length >= 1) {
				isSecond = true;
			}
			if (errorCode !== 0 || result === '' || undefined === result.data || result.data.length <= 0) {
				if (isSecond) {
					$('#view_result_condition_num').html('选出结果:<span class="cbla">0</span>个');
					$('#view_result_condition_table_tbody').html('<tr><td colspan="4">筛选结果为空</tr>');
					this.$el.show();
					return true;
				}

				$('#view_result_condition').hide();
				$('#noResultView').show();
				$('#defaultView').show();
				$('#view_result_condition_create').show();
				$('#view_result_condition_num').html('选出结果:<span class="cbla">0</span>个');
				$('#operate').show();
				return true;
			}
			return false;
		},
		hideEl: function() {
			this.$el.hide();
		},
		jumpClientMLine: function(e) {
			e = e || window.event;
			var code = $(e.currentTarget).attr('view_dbl_query_search');
			var stockList = querySearchModel.getQueryResultStr('|');
			clientJump.jumpClientStockPage(379, code, stockList);
		},
		//股票列表排序
		stockSort: function(e) {
			var self = this;
			e = e || window.event;
			var $dom = $(e.currentTarget);
			var type = $dom.attr("type"); //asc||desc
			var field = $dom.attr("field");
			var result = querySearchModel.get("result");
			var data = querySearchModel.sortData(field, type, result.data, 1);
			self.renderData(1, data);
			$(".sort").each(function() {
				$(this).html("");
			});
			if (type === 'asc') {
				$dom.attr("type", "desc");
				$dom.find(".sort").html("↑");
			} else {
				$dom.attr("type", "asc");
				$dom.find(".sort").html("↓");
			}
		}
	});
	module.exports = new QuerySearchView();
});
